﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace WebShopData
{
    public class Article
    {
        public int ArticleID { get; set; }
        public string ArticleName { get; set; }
        public int CategoryID { get; set; }
        public double UnitPrice { get; set; }
        public bool ActiveArticle { get; set; }
        public string ArticleImagePath { get; set; }

        public Article() { }
        public Article(string articleName, int categoryID, double unitPrice, bool activeArticle, string articleImagePath ,int articleID = 0)
        {
            ArticleID = articleID;
            ArticleName = articleName;
            CategoryID = categoryID;
            UnitPrice = unitPrice;
            ActiveArticle = activeArticle;
            ArticleImagePath = articleImagePath;
        }
        public void Load(SqlDataReader reader)
        {
            ArticleID = int.Parse(reader["ArticleID"].ToString());
            ArticleName = reader["ArticleName"].ToString().Trim();
            CategoryID = int.Parse(reader["CategoryID"].ToString());
            UnitPrice = double.Parse(reader["UnitPrice"].ToString());
            ActiveArticle = bool.Parse(reader["ActiveArticle"].ToString());
            ArticleImagePath = reader["ArticleImagePath"].ToString().Trim();
           
        }
        public override string ToString()
        {
            return string.Format("ArticleID:{0} ArticleName:{1} ArticleCategory:{2} UnitPrice:{3} ActiveArticle:{4} ArticleImagePath: {5}",
                ArticleID, ArticleName, CategoryID, UnitPrice, ActiveArticle, ArticleImagePath);
        }
    }
    public static class Articles
    {
        public static List<Article> GetArticles()
        {
            try
            {
                List<Article> articleList = new List<Article>();

                using (SqlConnection connection = DB.GetConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT * FROM Articles";
                        var adapter = new SqlDataAdapter(command);
                        var dataSet = new DataSet();
                        adapter.Fill(dataSet);

                        bool articleFound = dataSet.Tables[0].Rows.Count >= 1;
                        if (!articleFound)
                        {
                            return null;
                        }
                        for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
                        {
                            articleList.Add(new Article(
                                articleID: int.Parse(dataSet.Tables[0].Rows[i]["ArticleID"].ToString()),
                                articleName: dataSet.Tables[0].Rows[i]["ArticleName"].ToString().Trim(),
                                categoryID: int.Parse(dataSet.Tables[0].Rows[i]["CategoryID"].ToString()),
                                unitPrice: double.Parse(dataSet.Tables[0].Rows[i]["UnitPrice"].ToString()),
                                activeArticle: bool.Parse(dataSet.Tables[0].Rows[i]["ActiveArticle"].ToString()),
                                articleImagePath: dataSet.Tables[0].Rows[i]["ArticleImagePath"].ToString().Trim()
                                ));

                        }
                        return articleList;

                    }
                }
            }
            catch (Exception e)
            {
                DBException.AddException(e.Message, e.GetType().ToString());
                throw new DBException("Det gick tyvärr inte att hämta ut produkterna från databasen.",e);
            }
        }

        public static Article GetArticle(int articleID)
        {

            try
            {
                Article article = new Article();
                using (SqlConnection connection = DB.GetConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = string.Format("SELECT [ArticleID],[CategoryID],[ArticleName],[UnitPrice],[ArticleImagePath],[ActiveArticle] FROM [Articles] WHERE [ArticleID] = {0}", articleID);
                        SqlDataReader reader = command.ExecuteReader();
                        if (reader.Read())
                        {
                            article.Load(reader);
                        }

                    }
                    return article;
                }
            }
            catch (Exception e)
            {
                DBException.AddException(e.Message, e.GetType().ToString());
                throw new Exception();
            }
        }
    }
}
